package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.etl.model.EtlInput;
import com.jorado.event.EventClient;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.FileExporter;
import org.springframework.context.annotation.Description;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

@Description("基于文本的语料抽取服务")
public final class FileEtlService extends ExportToFileImpl<String> {

    private int total;
    private EtlService etlService;

    public FileEtlService(String sourceFile, String distFile) {
        this(sourceFile, distFile, true);
    }

    public FileEtlService(String sourceFile, String distFile, boolean deleteIfExists) {
        super(new FileExporter(sourceFile, true), distFile, deleteIfExists);
        this.etlService = new EtlService(this.distFile);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<String> dataList) {

        total += dataList.size();

        HashSet<EtlInput> etlList = new LinkedHashSet<>();

        String jobTitle = "";
        String jobType = "";
        String industry = "";

        for (String line : dataList) {

            String[] splits = line.split("……");

            if (splits.length > 0) {
                jobTitle = splits[0];
            }
            if (splits.length > 1) {
                jobType = splits[1];
            }
            if (splits.length > 2) {
                industry = splits[2];
            }

            EtlInput etlInput = new EtlInput();
            etlInput.setIndustry(industry);
            etlInput.setJobType(jobType);
            etlInput.setJobTitle(jobTitle);

            etlList.add(etlInput);

        }

        this.etlService.save(etlList);

    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取职位:[%d]个", total));

        return Result.OK;
    }
}
